<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\admin\model\CouponConsumer;
use app\admin\model\CouponOrder;
use app\admin\model\TicketAddress;
use app\admin\model\TicketConsumer;
use app\admin\model\TicketOrder;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Ticket extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Ticket();
    }


    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['ticket.is_delete','=',0];

        if ($this->request->platform_id >0 ){
            $where[] = ['ticket.platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['coupon.name','like',"%{$param['name']}%"];
        }

        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        $count = $this->model->withJoin(['platformInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin(['platformInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['start_time']  = date('Y-m-d H:i',$value['start_time']);
            $list[$key]['end_time']  = date('Y-m-d H:i',$value['end_time']);
            $list[$key]['desc']  = strip_tags($value['desc']);
            $ticketAddressModel = new TicketAddress();
            $ticketAddressList =  $ticketAddressModel->where(['ticket_id'=> $value['id']])->select();
            $list[$key]['address_list'] = $ticketAddressList;
         /*   $couponConsumerModel = new CouponConsumer();

            $couponConsumerList   = $couponConsumerModel->where(['ticket_id'=> $value['id'],'is_delete' => 0])->select()->toArray();
            $list[$key]['consume_id'] = array_column($couponConsumerList,'id');*/

        }

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function add()
    {
        $request = $this->request;
        $param = $request->param();
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'price' => $param['price'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'start_time' => strtotime($param['start_time']),
            'end_time' => strtotime($param['end_time']) + 86399,
        ];
        $this->model->save($data);
        $consumerModel = new \app\admin\model\Consumer();
        $consumerId  =[];
            foreach ($param['consume_id'] as $value){
                $consumerInfo = $consumerModel->where(['id' =>$value])->find();
                if (in_array($value,$consumerId)){
                    return msg([], 1, "每个店铺只能添加一张优惠券 : " );
                }
                $consumerId[] = $consumerInfo['store_id'];
                $couponConsumerData = [
                    'store_id' => $consumerInfo['store_id'],
                    'ticket_id' => $this->model->id,
                    'consumer_id'=>$value,
                    'platform_id' =>$this->request->platform_id,
                ];
                $couponConsumerModel = new CouponConsumer();
                $couponConsumerModel->save($couponConsumerData);
            }
            return  msg([], 0, "保存成功") ;
    }

    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $CouponOrderModel = new CouponOrder();
        $couponOrder= $CouponOrderModel->where(['ticket_id'=>  $param['id']])->find();
        if ($couponOrder){
            return  msg([], 1, "此卡券已有用户购买，请勿修改") ;
        }
        $data = [
            'platform_id' =>$this->request->platform_id,
            'name' => $param['name'],
            'price' => $param['price'],
            'desc' => htmlspecialchars_decode($param['desc']),
            'start_time' => strtotime($param['start_time']),
            'end_time' => strtotime($param['end_time']) + 86399,
        ];
        $this->model->where(['id' => $param['id']])->update($data);
        $consumerModel = new \app\admin\model\Consumer();
        $consumerId  =[];
        $couponConsumerModel = new CouponConsumer();
        $couponConsumerModel->where(['ticket_id'=>$param['id'] ])->update(['is_delete'  => 1]);
        foreach ($param['consume_id'] as $value){
            $consumerInfo = $consumerModel->where(['id' =>$value])->find();
            if (in_array($value,$consumerId)){
                return msg([], 1, "每个店铺只能添加一张优惠券" );
            }
            $consumerId[] = $consumerInfo['store_id'];
            $couponConsumerData = [
                'store_id' => $consumerInfo['store_id'],
                'ticket_id' => $param['id'],
                'consumer_id'=>$value,
                'platform_id' =>$this->request->platform_id,
            ];

            $couponConsumerModel->save($couponConsumerData);
        }
        return  msg([], 0, "保存成功") ;
    }

    public function ticketConsumer()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['ticket_consumer.is_delete', '=', 0];

        if ($this->request->platform_id > 0) {
            $where[] = ['platform_id', '=', $this->request->platform_id];
        }
        if (isset($param['name']) && $param['name']) {
            $where[] = ['ticketInfo.name', 'like', "%{$param['name']}%"];
        }

        if (isset($param['telphone']) && $param['telphone']) {
            $where[] = ['platformInfo.telphone', '=', $param['telphone']];
        }
        $couponConsumerModel = new TicketConsumer();
        $count = $couponConsumerModel
            ->withJoin(['platformInfo','ticketInfo','consumerInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $couponConsumerModel
            ->withJoin(['platformInfo','ticketInfo','consumerInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function removeTicketConsumer(){
        $request = $this->request;
        $param = $request->param();
        $CouponOrderModel = new CouponOrder();
        $couponConsumerModel = new CouponConsumer();
        $couponConsumerInfo = $couponConsumerModel->where(['id' =>$param['id'] ])->find();
        $couponOrder= $CouponOrderModel->where(['ticket_id'=>  $couponConsumerInfo['ticket_id']])->find();
        if ($couponOrder){
            return  msg([], 1, "此卡券已有用户购买，请勿修改") ;
        }
        $couponConsumerModel->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([], 0, "删除成功");
    }


    public function orderList(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];

        if ($this->request->platform_id >0 ){
            $where[] = ['ticket_order.platform_id','=',$this->request->platform_id];
        }
        $ticketOrder = new TicketOrder();
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        if (isset($param['name']) && $param['name']) {
            $where[] = ['ticketInfo.name', 'like', "%{$param['name']}%"];
        }
        $count = $ticketOrder->withJoin([
            'platformInfo',
            'ticketInfo',
            'userInfo'
        ], 'LEFT')
            ->where($where)
            ->count();
        $list = $ticketOrder->withJoin([
            'platformInfo',
            'ticketInfo',
            'userInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){
            if ($value['is_use'] == 1 && $value['end_time'] < time()){
                $list[$key]['is_use'] = 3;
            }
        }

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function delete(){


        $request = $this->request;
        $param = $request->param();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);
        $ticketOrderModel = new TicketOrder();
        $ticketOrderModel->whereIn('coupon_id', $param['id'])->update(['is_delete' => 1]);
        return msg([

        ], 0, "删除成功");
    }




}